
package CtrlLayer;
import ModelLayer.Task;
import ModelLayer.PartOrder;
import ModelLayer.Person;
import ModelLayer.Employee;
import DBLayer.IFDBTask;
import DBLayer.DBTask;
import java.util.ArrayList;

/**
 * @version 12/22/2011
 * @author Gruppe 4
 */

public class CtrlTask {
    private Task task;
    private PartOrder po;
    private CtrlPerson ctrlPerson;
    private IFDBTask dbTask;
    
    /**
     * Constructor for class CtrlTask
     */
    public CtrlTask()
    {
        task = new Task();
        po = new PartOrder();
        ctrlPerson = new CtrlPerson();
        dbTask = new DBTask();
    }
    
    /**
     * Method to find all tasks from the database
     * @param ()
     * @return Returns an ArrayList<Task>
     */
    public ArrayList getAllTasks(boolean retrieveAssociation)
    {
      ArrayList allTasks = new ArrayList();
      allTasks = dbTask.getAllTasks(true);
      return allTasks;
    }
    
    /**
     * Method to find a Task object in the database
     * @param taskId int taskId the taskId of the task
     * @param retreiveAssociation boolean
     * @return returns an Object of type Task
     */
    public Task findTaskById(int taskId, boolean retrieveAssociation)
    {
        return dbTask.searchTaskId(taskId,true);
    }
    
    /**
     * Method to find a Task object in the database by date
     * @param startDate String startDate the startDate of the Task
     * @param retreiveAssociation boolean
     * @return returns an Object of type Task
     */
    public Task findTaskByDate(String startDate, boolean retrieveAssociation)
    {
        return dbTask.searchTaskDate(startDate,true);
    }
    
    /**
     * Method to insert a Task object into the database
     * @param task an Object of the type Task
     * @param orderId int
     * @return returns an int 1 if the method succedes and -1 if the method fails
     */
    public void insertNewTask(Task task, int orderId) throws Exception
      {
          dbTask.insertTask(task, orderId);
      }
    
    /**
     * Method to insert a PartOrder object into the database
     * @param partOrder an Object of the type PartOrder
     * @return returns an int 1 if the method succedes and -1 if the method fails
     */
    public void insertNewPartOrder(PartOrder partOrder) throws Exception
    {
        task.getTaskId();
        
        dbTask.insertPartOrder(partOrder);
    }
    
    /**
     * Method to update a task in the database
     * @param name String name the name of the task
     * @param disclaimer String disclaimer the disclaimer of task
     * @param startDate String startDate the startDate of the task
     * @param endDate String endDate the endDate of the task
     * @return returns an int 1 if the method succedes and -1 if the method fails
     */
    public int updateTask(String name, String disclaimer, String startDate, String endDate)
      {
          Task taskObj = new Task();
          taskObj.setName(name);
          taskObj.setDisclaimer(disclaimer);
          taskObj.setStartDate(startDate);
          taskObj.setEndDate(endDate);
          return  dbTask.updateTask(taskObj);
      }
 
    /**
     * Method to delete a Task from the database
     * @param taskId int taskId the id of the Task
     * @return returns an int 1 if the method succedes and -1 if the method fails
     */
    public void deleteTask(int taskId )
      {
          dbTask.deleteTask(taskId);          
      }
    
    /**
     * Method to create a Task
     * @param taskName String taskName the name of the Task
     * @param disclaimer String disclaimer the disclaimer of the Task
     * @param startDate String startDate the startDate of the Task
     * @param endDate String endDate the endDate of the Task
     * @return returns an Object of type Task
     */
    public Task createTask(String taskName, String disclaimer, String startDate, String endDate)
    { 
        task.setName(taskName);
        task.setDisclaimer(disclaimer);
        task.setStartDate(startDate);
        task.setEndDate(endDate);
        
        System.out.println(task);
        
        return task;
    }
    
    /**
     * Method to create a new Object a Partorder
     * @param employeeId int employeeId id of an employee
     * @return returns an Object of type PartOrder
     */
    public PartOrder addEmployee(int employeeId, double amount)
    {
        Person p = ctrlPerson.findPersonById(employeeId, true);
        System.out.println(p);
        
        po = setPartOrder(p, amount);
        task.setPartOrders(po);
        
        return po;
    }
    
    /**
     * Method to create a new Object a Partorder
     * @param an Object person of type Person
     * @param amount double
     * @return returns an Object of type PartOrder
     */
    private PartOrder setPartOrder(Person person, double amount)
    {
        Employee employee = (Employee) person;
        PartOrder partOrder = new PartOrder();
        
        partOrder.setEmployee(employee);
        partOrder.setAmount(amount);
        
        System.out.println(partOrder);
        return partOrder;
    }
}